﻿using LiteBlog.Common.Components;
using LiteBlog.Common.Entities;
using LiteBlog.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LiteBlog.Controllers.Filters
{
    public class CrawlerFilterForPost : ActionFilterAttribute 
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var controller = filterContext.Controller as BaseController;
            // a hack since M$ does not identify Facebook scrapper as a crawler
            bool isCrawler = controller.Request.Browser.Crawler;
            if (controller.Request.UserAgent != null)
                if (controller.Request.UserAgent.Contains("facebookexternalhit"))
                    isCrawler = true;
            if (isCrawler)
            {
                // If you are a crawler, I got you a special page!
                var post = filterContext.RouteData.Values["post"] as Post;
                var viewName = "Crawler" + filterContext.ActionDescriptor.ActionName;
                filterContext.Result = controller.GetViewResult(viewName, post);
            }
            else
            {
                // If you are not a crawler, go to the cool page that we got.
                base.OnActionExecuted(filterContext);
            }
        }
    }
}